﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media.Effects;
using System.Windows.Threading;
using Cjc.ThreeDeemium.Effects;
using Cjc.ThreeDeemium.Properties;
using System.Windows.Media.Animation;

namespace Cjc.ThreeDeemium
{
	/// <summary>
	/// Interaction logic for Window1.xaml
	/// </summary>
	public partial class Window1 : Window
	{
		private string baseTitle;
		private BindingBase addressBinding;

		public Window1()
		{
			Effects = EffectsHelper.GetEffects();

			InitializeComponent();

			address.GotFocus += delegate { address.SelectAll(); };
			Loaded += delegate { address.Focus(); };
			Closing += delegate { Settings.Default.Save(); };

			// Preserve binding; we'll need to set it again after input
			addressBinding = BindingOperations.GetBinding( address, TextBox.TextProperty );
			baseTitle = Title;

			browser.Ready += delegate
			{
				browser.BeginLoading += delegate { loading.Visibility = Visibility.Visible; };
				browser.FinishLoading += delegate { loading.Visibility = Visibility.Collapsed; };
			};

			browser.Source = Settings.Default.DefaultUrl;
            browser.ChangeTargetUrl += new EventHandler<Cjc.ChromiumBrowser.WebBrowser.ChangeTargetUrlEventArgs>(browser_ChangeTargetUrl);
		}

        /// <summary>
        /// This method called when ever the mouse moves over an element. 
        /// This method can be used to cancel a click if the URL does not meet a certain condition.
        /// If used this way, you would let this method update a variable with the URL that the mouse is currently pointing at. 
        /// We must then also ensure, that mouseclicks are NOT forwarded to the webbrowser automatically, but in stead captured by your own method.
        /// That method would then look at the URL the mouse is over (thanks to this method below) and then decide if the click should be forwarded to the browser element or not
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void browser_ChangeTargetUrl(object sender, Cjc.ChromiumBrowser.WebBrowser.ChangeTargetUrlEventArgs e)
        {
            Console.WriteLine("Mouse is now over: " + e.Url);
        }

		public IDictionary<string, ShaderEffect> Effects { get; private set; }

        private void Button_Click( object sender, RoutedEventArgs e )
        {
			var url = GetUrl();

			if ( url.StartsWith( "javascript:" ) )
			{
				browser.ExecuteJavascript( url.Replace( "javascript:", "" ), "" );
			}
			else
			{
				browser.Navigate( GetUrl() );
				browser.Focus();
			}

			// Replace binding (since Mode=OneWay, text input will clear binding)
			address.SetBinding( TextBox.TextProperty, addressBinding );
		}

		private void browser_ReceiveTitle( object sender, Cjc.ChromiumBrowser.WebBrowser.ContentEventArgs e )
		{
			if ( string.IsNullOrEmpty( e.FrameName ) ) this.Title = e.Content + " - " + baseTitle;
		}

		private void browser_Status( object sender, Cjc.ChromiumBrowser.WebBrowser.StatusEventArgs e )
		{
			status.Items.Insert( 0, e.Message );
		}

		private void openTransparent_Click( object sender, RoutedEventArgs e )
		{
			var childWindow = new ChildWindow( GetUrl() );
			childWindow.Show();
		}

		private void back_Click( object sender, RoutedEventArgs e )
		{
			browser.GotoHistoryOffset( -1 );
		}

		private void forward_Click( object sender, RoutedEventArgs e )
		{
			browser.GotoHistoryOffset( 1 );
		}

		private void Default_Click( object sender, RoutedEventArgs e )
		{
			Settings.Default.DefaultUrl = GetUrl();
		}

		private string GetUrl()
		{
			var source = address.Text.Trim();

			return source.Contains( ":" ) ? source : "http://" + source;
		}
	}
}